package com.example.mrzhou.lvmm.commom.utils;

import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;

import java.util.List;

/**
 * Created by Mr.zhou on 2016/10/19.
 */

public class FragmentViewPagerUtils {

    private List<Fragment> fragments;
    private String[] strings;
    private List<String> lists;
    /**
     * 不能使用单例模式
     */
    public FragmentViewPagerUtils() {

    }

    public void fragmentVpUtils(List<Fragment> fragments, String[] strings, ViewPager vp, TabLayout tb, FragmentManager fm){
        this.fragments =fragments;
        this.strings =strings;
        VpAdapter adapter = new VpAdapter(fm);
        vp.setAdapter(adapter);
        tb.setupWithViewPager(vp);
    }

    public class VpAdapter extends FragmentStatePagerAdapter {

        public VpAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return fragments != null ? fragments.get(position) : null;
        }

        @Override
        public int getCount() {
            return fragments != null ? fragments.size() : 0;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return strings[position];
        }
    }

    public void fragmentVpUtils(List<Fragment> fragments, List<String> lists, ViewPager vp, TabLayout tb, FragmentManager fm){
        this.fragments =fragments;
        this.lists =lists;
        VplistAdapter adapter = new VplistAdapter(fm);
        vp.setAdapter(adapter);
        tb.setupWithViewPager(vp);
    }

    public class VplistAdapter extends FragmentStatePagerAdapter {

        public VplistAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return fragments != null ? fragments.get(position) : null;
        }

        @Override
        public int getCount() {
            return fragments != null ? fragments.size() : 0;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return lists.get(position);
        }
    }
}
